﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Text;
using System.Xml;

#endregion


namespace ESF.General
{
	public static class SerializeHelper
	{
		#region Serialize XML

		public static T DeSerializeXML<T>(string pClassXMLSerialized, params Type[] pExtraTypes)
		{
			return DeSerializeXML<T> (new StringReader(pClassXMLSerialized), pExtraTypes);
		}


		public static T DeSerializeXML<T>(StringReader pClassSerializedReader, params Type[] pExtraTypes)
		{
			XmlSerializer xmlSerializer;


			xmlSerializer = new XmlSerializer(typeof(T), pExtraTypes);

			try
			{
				return (T) xmlSerializer.Deserialize (pClassSerializedReader);
			}
			catch (Exception excEx)
			{
				throw new FormatException("No fue leido el objeto solicitado", excEx);
			}
		}


		public static T DeSerializeFromFileXML<T>(string pFilename, params Type[] pExtraTypes)
		{
			XmlSerializer xmlSerializer;


			xmlSerializer = new XmlSerializer(typeof(T), pExtraTypes);

			if (!File.Exists (pFilename))
			{
				return default (T);
			}

			return (T) xmlSerializer.Deserialize(File.OpenRead (pFilename));
		}


		public static string SerializeXML<T>(T pObject, params Type[] pExtraTypes)
		{
			StringBuilder stbXML;


			stbXML = new StringBuilder();

			SerializeXML <T> (pObject, TextWriter.Synchronized (new StringWriter (stbXML)), pExtraTypes);

			return stbXML.ToString ();
		}


		public static void SerializeXML<T>(T pObject, TextWriter pTextWriter, params Type [] pExtraTypes)
		{
			XmlSerializer xmlSerializer;

			XmlWriter xmlWriter;


			xmlWriter = XmlWriter.Create (pTextWriter);

			xmlSerializer = new XmlSerializer (typeof(T), pExtraTypes);

			xmlSerializer.Serialize (xmlWriter, pObject);
		}


		public static void SerializeToFileXML<T>(T pObject, string pFilename, params Type[] pExtraTypes)
		{
			XmlSerializer xmlSerializer;

			StreamWriter stmFile;

			XmlWriter xmlWriter;


			FileHelper.Delete(pFilename);

			using (stmFile = File.CreateText (pFilename))
			{
				xmlWriter = XmlWriter.Create (stmFile);

				xmlSerializer = new XmlSerializer(typeof(T), pExtraTypes);

				xmlSerializer.Serialize (xmlWriter, pObject);
			}
		}
		
		#endregion
		
		
		#region Serialize Binary
		
		public static T DeSerializeBin <T>(MemoryStream pMemStream)
		{
			BinaryFormatter binFormater;
			
			
			binFormater = new BinaryFormatter ();
			
			return (T) binFormater.Deserialize (pMemStream); 
		}
		
		
		public static T DeSerializeFromFileBin <T>(string pFilename)
		{
			BinaryFormatter binFormater;
			
			FileStream fs;
			
			
			if (!File.Exists (pFilename))
			{
				return default (T);
			}

			binFormater = new BinaryFormatter ();
			
			using (fs = File.OpenRead (pFilename))
			{
				return (T) binFormater.Deserialize (fs);
			}
		}


		public static MemoryStream SerializeBin<T>(T pObject)
		{
			BinaryFormatter binFormater;
			
			MemoryStream ms;
			
			
			binFormater = new BinaryFormatter ();
			ms = new MemoryStream ();  
			
			binFormater.Serialize (ms, pObject);

			return ms;
		}


		public static void SerializeToFileBin<T>(T pObject, string pFilename)
		{
			StreamWriter stmFile;


			FileHelper.Delete (pFilename);
		
		
			using (stmFile = File.CreateText (pFilename))
			{
				SerializeBin (pObject).WriteTo (stmFile.BaseStream);
			}
		}
		
		#endregion
	}
}
